package com.yk.faceapplication.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.ViewSwitcher.ViewFactory;

import com.allinpay.xed.R;

/**
 * Created by yk_2018/1/30_10:19
 */

public class GuideActivity extends BaseActivity implements View.OnTouchListener {
    private ImageSwitcher mImageSwitcher;
    private Button loginBtn;
    private int[] imgIds;

    /**
     * 当前选中的图片id序号
     */
    private int currentPosition = 0;
    /**
     * 按下点的X坐标
     */
    private float downX;


    @Override
    public void onClick(View v) {
        super.onClick(v);
        if(v.getId() == R.id.login_btn){
            startActivity(new Intent(GuideActivity.this,HomeActivity.class));
            finishAct();
        }
    }

    @Override
    public boolean onKeyDown(int keyCode,KeyEvent event){
        if(keyCode==KeyEvent.KEYCODE_BACK)
            return true;//不执行父类点击事件
        return super.onKeyDown(keyCode, event);//继续执行父类其他点击事件
    }

    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        imgIds = new int[]{R.drawable.guide_1,R.drawable.guide_2,R.drawable.guide_3};
        loginBtn  = (Button) findViewById(R.id.login_btn);
        loginBtn.setOnClickListener(this);
        //实例化ImageSwitcher
        mImageSwitcher  = (ImageSwitcher) findViewById(R.id.imageSwitcher);
        mImageSwitcher.setFactory(new ViewFactory() {

            @Override
            public View makeView() {
                // TODO Auto-generated method stub
                ImageView imageView = new ImageView(GuideActivity.this);
//                imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                imageView.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

//                imageView.setOnClickListener(new View.OnClickListener() {
                //                    @Override
                //                    public void onClick(View v) {
                //                        if(currentPosition == (imgIds.length - 1)){
                //                            startActivity(new Intent(GuideActivity.this,HomeActivity.class));
                //                            finishAct();
                //                        }
                //                    }
                //                });
                return imageView;
            }
        });
        mImageSwitcher.setInAnimation(this, android.R.anim.fade_in);
        mImageSwitcher.setOutAnimation(this, android.R.anim.fade_out);
        mImageSwitcher.setImageResource(imgIds[currentPosition]);
        mImageSwitcher.setOnTouchListener(this);

    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:{
                //手指按下的X坐标
                downX = event.getX();
                break;
            }
            case MotionEvent.ACTION_UP:{
                float lastX = event.getX();
                //抬起的时候的X坐标大于按下的时候就显示上一张图片
                if(lastX > downX){
                    if(currentPosition > 0){
                        //设置动画，这里的动画比较简单，不明白的去网上看看相关内容
                        mImageSwitcher.setInAnimation(AnimationUtils.loadAnimation(getApplication(), R.anim.in_from_left));
                        mImageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(getApplication(), R.anim.out_from_right));
                        currentPosition --;
                        mImageSwitcher.setImageResource(imgIds[currentPosition % imgIds.length]);
                        loginBtn.setVisibility(View.GONE);
                    }else{
//                        Toast.makeText(getApplication(), "已经是第一张", Toast.LENGTH_SHORT).show();
                    }
                }

                if(lastX < downX){
                    if(currentPosition < imgIds.length - 1){
                        mImageSwitcher.setInAnimation(AnimationUtils.loadAnimation(getApplication(), R.anim.in_from_right));
                        mImageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(getApplication(), R.anim.out_from_left));
                        currentPosition ++ ;
                        mImageSwitcher.setImageResource(imgIds[currentPosition]);
                        if(currentPosition == (imgIds.length - 1)){
                            loginBtn.setVisibility(View.VISIBLE);
                        }
                    }else{
//                        Toast.makeText(getApplication(), "到了最后一张", Toast.LENGTH_SHORT).show();
                    }
                }
            }

            break;
        }

        return true;
    }


    @Override
    protected void onHandleMessage(Message msg) {
        super.onHandleMessage(msg);
    }
}
